import FocusParentObj from "../Common/FocusParentObj";
import LevelBaseControl from "../../../LevelCommon/LevelBaseControl";

export default class TouYingObj extends Laya.Script3D{
    private focusObj:FocusParentObj;
    /** 鼠标控制旋转速度*/
    public  mouseControlSpeed:number = 100;
    /** 触屏控制旋转速度*/
    public  touchControlSpeed:number = 0.1;
    /** 允许偏差度数*/
    public  offAngle:number = 10;
    /** 当前是否投影正确了*/
    private isCorrect:boolean = false;

    private lastMoveX: number;

    private lastMoveY: number;

    private mowner:Laya.Sprite3D;

    onStart()
    {        
        this.mowner = this.owner as Laya.Sprite3D;
        this.focusObj = this.owner.parent.parent.parent.getComponent(FocusParentObj);
        // this.InitMouseEvent();
        let levelControl:LevelBaseControl = window.gameFacade.LevelMng.GetCurLevelControl();
        levelControl.DoInteractObjLogic(this.owner.name + "_Unlock");
    }

    private InitMouseEvent() {
        Laya.stage.on(Laya.Event.MOUSE_DOWN, this, this.MouseHandler);
        Laya.stage.on(Laya.Event.MOUSE_UP, this, this.MouseHandler);
        Laya.stage.on(Laya.Event.MOUSE_OUT, this, this.MouseHandler);
    }

    private MouseHandler(e: Laya.Event) {
        var touches: Array<any> = e.touches;

        switch (e.type) {
            case Laya.Event.MOUSE_DOWN:
                {
                    if (touches && touches.length == 1) {
                        this.lastMoveX = touches[0].stageX;
                        this.lastMoveY = touches[0].stageY;
                    }

                    Laya.stage.on(Laya.Event.MOUSE_MOVE, this, this.MouseHandler);
                    break;
                }
            case Laya.Event.MOUSE_MOVE:
                {
                    if (!this.focusObj.isInteractive){
                        return;
                    }
                    if (touches && touches.length == 1) {
                        let currentX = touches[0].stageX;
                        let currentY = touches[0].stageY;

                        let deltaX = this.lastMoveX - currentX;
                        let deltaY = this.lastMoveY - currentY;
                        let rotateValue: Laya.Vector3 = new Laya.Vector3(
                            0,
                            -deltaX * this.touchControlSpeed * Laya.timer.delta / 1000,
                            -deltaY * this.touchControlSpeed * Laya.timer.delta / 1000
                           );
                        this.lastMoveX = touches[0].stageX;
                        this.lastMoveY = touches[0].stageY;
                        this.RotateObj(rotateValue);
                    }
                    break;
                }
            case Laya.Event.MOUSE_OUT:
            case Laya.Event.MOUSE_UP:
                {
                    Laya.stage.off(Laya.Event.MOUSE_MOVE, this, this.MouseHandler);
                    break;
                }
        }
    }

   
    public  RotateObj( rotateAngle:Laya.Vector3)
    {
        return;
        this.mowner.transform.rotate(rotateAngle,false,true);
        /** 检测结果*/
        let curAngles:Laya.Vector3 = this.mowner.transform.localRotationEuler.clone();
        let angleX = curAngles.x;
        angleX = angleX > 180 ? Math.abs(angleX - 360) : angleX;
        let angleY = curAngles.y; 
        angleY = angleY > 180 ? Math.abs(angleY - 360) : angleY;
        let angleZ = curAngles.z;
        angleZ = angleZ > 180 ? Math.abs(angleZ - 360) : angleZ;
        /** 小于误差值  就说明对了*/
        if (angleX <= this.offAngle && angleY <= this.offAngle && angleZ <= this.offAngle)
        {
            console.log("投影正确！！！！！！！");
            let levelControl:LevelBaseControl = window.gameFacade.LevelMng.GetCurLevelControl();
            levelControl.DoInteractObjLogic(this.owner.name + "_Unlock");
            this.isCorrect = true;
        }                
    }

    onDestroy() {
        Laya.stage.off(Laya.Event.MOUSE_DOWN, this, this.MouseHandler);
        Laya.stage.off(Laya.Event.MOUSE_UP, this, this.MouseHandler);
        Laya.stage.off(Laya.Event.MOUSE_MOVE, this, this.MouseHandler);
        Laya.stage.off(Laya.Event.MOUSE_OUT, this, this.MouseHandler);
        Laya.stage.off(Laya.Event.MOUSE_WHEEL, this, this.MouseHandler);
    }
}